/*
 * ASTAbstractNode.cpp
 *
 *  Created on: 2010-6-3
 *      Author: Guobc
 */

#include "ASTAbstractNode.h"
using namespace HA;

ASTAbstractNode::ASTAbstractNode() :
	HA::ASTNode(), mParent(NULL), children(), startLine(0), endLine(0) {
}

ASTAbstractNode::ASTAbstractNode(ASTDom* document, int start, int end) :
	HA::ASTNode(), mParent(NULL), children(), startLine(start), endLine(end) {
}

ASTNode* ASTAbstractNode::getPreviousSibling() {
	ASTNode* parent = this->getParent();
	if (parent == NULL)//root node
		return NULL;
	ASTNodeList sblings = parent->getChildren();
	int size = sblings.size();
	if (size < 2) {// no sibling
		return NULL;
	}

	int pos = -1;
	for (int i = 0; i < size; i++) {
		if ((*sblings.item(i)) == (*this)) {
			pos = i;
			break;
		}
	}
	if (pos < 1) {
		return NULL;
	}

	return sblings.item(pos - 1);
}

ASTNode* ASTAbstractNode::getNextSibling() {
	ASTNode* parent = this->getParent();
	if (parent == NULL)//root node
		return NULL;
	ASTNodeList sblings = parent->getChildren();
	int size = sblings.size();
	if (size < 2) {// no sibling
		return NULL;
	}

	int pos = -1;
	for (int i = 0; i < size; i++) {
		if ((*sblings.item(i)) == (*this)) {
			pos = i;
			break;
		}
	}
	if (pos < 1) {
		return NULL;
	}

	return sblings.item(pos + 1);
}

ASTAbstractNode::~ASTAbstractNode() {
}
